package com.fau.cm.action.index;

import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.cm.action.PublicAction;
import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalCall;
import com.fau.cm.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 组长分配Action 
 * @author kangyj
 * @datetime 2014年10月29日,上午10:01:35
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class CallAllocateGroupAction extends PublicAction implements
		ModelDriven<MedicalCall> {

	private static final long serialVersionUID = 3720455727340291954L;
	
	private MedicalCall call = new MedicalCall();
	
	/**
	 * @desc 组长话务分配页面初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月29日,上午10:04:17
	 */
	@Action(value = "call_allocate_group_init", results = {@Result(location = "call_allocate_group_init.jsp")})
	public String init() {
		return SUCCESS;
	}
	
	/**
	 * @desc 分页查询未分配任务
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月29日,上午10:04:43
	 */
	@Action(value = "call_allocate_group_view")
	public String view() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ServletActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		pageBean = this.baseAllService.getCallGroupManagerService().findCallByPage(call, admin, map);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 查询当前人所在组话务员集合
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月29日,上午11:15:30
	 */
	@Action(value = "call_allocate_group_find_user")
	public String findUserList() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getCallGroupManagerService().findUserList(admin);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 批量分配到话务员
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月29日,上午11:30:08
	 */
	@Action(value = "call_allocate_group_user")
	public String allocateUser() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(call.getId()) && StringUtils.isNotBlank(call.getHandleUserId())) {
			boolean flag = this.baseAllService.getCallGroupManagerService().callAllocate(call.getId(), call.getHandleUserId());
			if(flag) {
				map.put("success", "true");
			} else {
				map.put("success", "error");
			}
		}else {
			map.put("success", "false");
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 批量修改类别
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月23日,下午2:07:46
	 */
	@Action(value = "call_allocate_group_cat")
	public String editCategory(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(call.getId()) && StringUtils.isNotBlank(call.getCategory())){
			this.baseAllService.getCallGroupManagerService().editCallCategory(call.getId(), call.getCategory());
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 批量修改任务状态
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月25日,下午4:59:10
	 */
	@Action(value = "call_allocate_group_sta")
	public String editStatus() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(call.getId()) && StringUtils.isNotBlank(call.getPocessingStatus())) {
			this.baseAllService.getCallGroupManagerService().editCallStatus(call.getId(), call.getPocessingStatus());
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 删除话务任务
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月19日,下午7:25:29
	 */
	@Action(value = "call_allocate_group_del")
	public String del(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(call.getId())){
			this.baseAllService.getCallGroupManagerService().delCall(call);
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 导出全部
	 * @return null
	 * @author liy
	 * @datetime 2015年1月4日,下午3:46:27
	 */
	@Action(value = "call_allocate_group_export_all")
	public String exportAll(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"组长未分配话务任务.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			Workbook workbook = this.baseAllService.getCallGroupManagerService().exportAll(admin,this.call);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 导出当前
	 * @return null
	 * @author liy
	 * @datetime 2015年1月4日,下午3:55:25
	 */
	@Action(value = "call_allocate_group_export_cur")
	public String unaExportCurrent(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"组长未分配话务任务.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			if(StringUtils.isNotBlank(call.getId())){
				Workbook workbook = this.baseAllService.getCallGroupManagerService().exportCurrent(call.getId());
				workbook.write(toClient);
			}
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public MedicalCall getModel() {
		return call;
	}

}
